/**  
  -- The Performance Driven Lime Project -- 

  A self adaptive tuple space for mobile agents and hosts by dynamic
  adapting tuple's replication policy.
  
  Copyright (C) 2008
  Xing Jiankuan, Pan Wenhui
  SAGroup of Tsinghua University

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */
package sagroup.dracots.testing;

import java.util.HashSet;

import sagroup.dracots.DracoTS;
import sagroup.dracots.DracoTuple;
import sagroup.dracots.FlatShareTuple;
import sagroup.dracots.ShareTuple;
import sagroup.dracots.TupleType;

import lights.adapters.Field;
import lights.adapters.Tuple;
import lights.interfaces.ITuple;

/**
 * @author Xing Jiankuan
 *
 */
public class ContainmentAgent implements Runnable {

	private long period = 30000;
	
	private int readPort = 40000;
	
	private String tsname = "zhengxiang";
	
	private DracoTS dts = null;
	
	private HashSet<ShareTuple> tupleset = new HashSet();
	/**
	 * 
	 */
	public ContainmentAgent() {
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		// TODO Auto-generated method stub
       System.out.println("Agent has started prepared...");
	   //prepare
		 
	   dts = DracoTS.get(tsname);
	   for(int i=1; i <= 20; i++)
	   {
		   ITuple it = new Tuple();
			  it.add(new Field().setValue(i)); 
	       FlatShareTuple tuple = new FlatShareTuple(new TupleType("A"), it); 
	       tupleset.add(tuple);
	       dts.out(tuple);
	       System.out.println("Agent has out tuple id: "+ tuple.getVID());
	    }
		
		try
		{
		Thread.sleep(period * 5);
		}
		catch(InterruptedException iex)
		{
			System.out.println(iex.getMessage());
		}
		
//		//A phase update all tuples
//		System.out.println("Agent has started A phase.");
//	    for(ShareTuple dt : tupleset)
//		{
//			try
//			{
//			Thread.sleep(1000);
//			}
//			catch(InterruptedException iex)
//			{
//				System.out.println(iex.getMessage());
//			}
//		    dts.update(dt);
//		    System.out.println("A : Agent has updated tuple id: "+ dt.getVID());
//		}
//		
//		//B phase do nothing
//	    System.out.println("Agent has started B phase.");
//		try
//		{
//		Thread.sleep(period * 10);
//		}
//		catch(InterruptedException iex)
//		{
//			System.out.print(iex.getMessage());
//		}
		//C phase== A phase
		System.out.println("Agent has started C phase.");
		 for(ShareTuple dt : tupleset)
			{
				try
				{
				Thread.sleep(5000);
				}
				catch(InterruptedException iex)
				{
					System.out.println(iex.getMessage());
				}
			   // dts.update(dt);
			    //System.out.println("C: Agent has update tuple id: "+ dt.getVID());
			}
		 
		 System.out.println("Agent has started D phase.");
		//D phase delete all tuples
		
			try
			{
			Thread.sleep(period * 4);
			}
			catch(InterruptedException iex)
			{
				System.out.print(iex.getMessage());
			}
		 
		 for(DracoTuple dt : tupleset)
		{
			try
			{
			Thread.sleep(1000);
			}
			catch(InterruptedException iex)
			{
				System.out.print(iex.getMessage());
			}
		  //  dts.inp(dt);
		   // System.out.println("Agent has in tuple id: "+ dt.getVID());
		}
		
		System.out.println("Agent has finished all work.");
		
	}

}
